package com.maimao.user.controller;

import com.maimao.api.user.ShopApi;
import com.maimao.core.constant.Constants;
import com.maimao.core.model.resp.Result;
import com.maimao.model.common.StatusInput;
import com.maimao.model.user.Shop;
import com.maimao.model.user.input.ShopBindBrand;
import com.maimao.user.controller.base.BaseController;
import com.maimao.user.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author MaoLin Wang
 * @date 2020/12/22 7:39 下午
 */
@RestController
public class ShopController extends BaseController implements ShopApi {

    @GetMapping("/by-owner")
    @Override
    public Result<?> findShopByOwner(@RequestParam("owner") Long owner, @RequestParam("social") Integer social) {
        if (owner == null || owner < 1 || social == null || !(social.equals(Constants.ONE) || social.equals(Constants.ZERO))) {
            return Result.notValid();
        }

        return Result.okData(shopService.findShopByOwner(owner, social));
    }

    @Override
    @GetMapping("/one")
    public Result<?> queryById(@RequestParam("id") Long id) {
        if (id == null || id < 0) {
            return Result.notValid();
        }
        return Result.okData(shopService.queryById(id));
    }

    /**
     * 查询待审核的店铺列表
     *
     * @param shopType 1 企业店铺 2 普通店铺 目前只支持2
     */
    @Override
    @GetMapping("/shop2Check")
    public Result<?> queryToCheck(@RequestParam(value = "shopType", required = false, defaultValue = "2") Integer shopType,
                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "20") Integer size) {
        return Result.okData(shopService.queryToCheck(shopType, page, size));
    }

    /**
     * 审核普通用户店铺开通申请
     *
     * @param shopType 1 企业店铺 2 普通店铺 目前只支持2
     */
    @Override
    @PostMapping("/check")
    public Result<?> check(@Validated @RequestBody StatusInput input, @RequestParam("shopType") Integer shopType) {
        if (shopType == null) {
            return Result.notValid();
        }
        shopService.check(input.getStatus(), input.getId(), shopType);
        return Result.ok("审核成功");
    }

    /**
     * 店铺绑定品牌（品牌审核接口内部调用）
     */
    @PostMapping("/client/bindBrand")
    @Override
    public Result<?> bindBrand(@RequestBody @Validated ShopBindBrand shopBindBrand) {
        shopService.bindBrand(shopBindBrand.getBrandId(),shopBindBrand.getShopId());
        return Result.ok("品牌绑定成功");
    }

    /**
     * 暂时不知道如何专门对内部无认证的请求放行 单拉一个放行用于内部调用
     */
    @GetMapping("/one/mmin")
    public Result<?> queryByIdIn(@RequestParam("id") Long id) {
        if (id == null || id < 0) {
            return Result.notValid();
        }
        return Result.okData(shopService.queryById(id));
    }

    /**
     * 根据店铺id查询用户邮箱(商品审核后需要根据店铺id查询邮箱进行发送，目前仅这一处内部调用)
     *
     * @param id 店铺id
     * @return
     */
    @GetMapping("/queryUserEmailById")
    public Result<?> queryUserEmailById(@RequestParam("id") Long id) {
        if (id == null || id < 0) {
            return Result.notValid();
        }
        return Result.okData(shopService.queryUserEmailById(id));

    }

    @Autowired
    private ShopService shopService;
}
